package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 菜品模块
@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    // 分页+条件查询
    @GetMapping("/dish/page")
    public ResultInfo findByPage(
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            String name) { // 1.接收参数
        // 2.调用service
        Page<Dish> page = dishService.findByPage(pageNum, pageSize, name);
        // 3.返回结果
        return ResultInfo.success(page);
    }

    //新增菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){ //1.接收post请求参数
        //2.调用service新增
        dishService.save(dish);
        //3.返回结果
        return ResultInfo.success();
    }
    //回显菜单
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable Long id){ //1.接收参数
        //2.调用service查询
   Dish dish =  dishService.findById(id);
   //3.返回结果
        return  ResultInfo.success(dish);
    }
    //修改菜品
    @PutMapping("/dish")
    public ResultInfo updateById(@RequestBody Dish dish){ //1.接收put请求参数
        //2.调用service修改
         dishService.updateById(dish);
         //3.返回结果
        return ResultInfo.success();

    }
    //批量删除菜品
    @DeleteMapping("/dish")
    public ResultInfo deleteBathIds(@RequestParam List<Long> ids){ //接收delete删除
        //2.调用service删除
        dishService.deleteBathIds(ids);
        //3.返回结果
        return ResultInfo.success();
    }
    //停售起售
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status,
                                   @RequestParam List <Long> ids){ //接收post启停售
    dishService.updateStatus(status,ids);
    //返回结果
        return ResultInfo.success();
    }
    //根据分类查询菜品列表
    @GetMapping("/dish/list")
    public ResultInfo findListByCategoryId(Long categoryId,String name){ //1.接收参数
    //2.调用service查询
       List<Dish> dishList =  dishService.findListByCategoryId(categoryId,name);
   //3.返回结果
        return ResultInfo.success(dishList);
    }

}